package com.zsy.base.bean;

import com.zsy.base.constant.Constants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: springboot_demo
 * @description: 结果封装类
 * @author: zhaoshouyun
 * @create: 2020-01-17 17:45
 **/
@Data
@NoArgsConstructor
@ApiModel(value="结果封装体：DemoResult，点击查看详情", description="接口返回统一包装体")
public class DemoResult<T> {

    /**
     *code  0成功  非0均为失败
     **/
    @ApiModelProperty(value="返回码，0标识成功，非零失败",required = true)
    private String code;
    /**
     * msg  失败信息
     **/
    @ApiModelProperty(value="返回信息",required = false)
    private String msg;
    /**
     * data  数据
     **/
    @ApiModelProperty(value="返回数据",required = false)
    private T data;

    public DemoResult(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public DemoResult(String code, T data) {
        this.code = code;
        this.data = data;
    }

    public boolean isSuccess(){
        if (Constants.SUCCESS.equals(this.code)){
            return true;
        }else{
            return false;
        }
    }

    public static <T> DemoResult SUCCESS(String msg, T data){
        return new DemoResult<T>(Constants.SUCCESS,msg,data);
    }
    public static <T> DemoResult SUCCESS(T data){
        return SUCCESS("操作成功",data);
    }
    public static <T> DemoResult SUCCESS(){
        return SUCCESS("操作成功",null);
    }
    public static DemoResult FAIL(){
        return FAIL("操作失败",null);
    }
    public static <T> DemoResult FAIL(String msg){
        return FAIL(msg,null);
    }

    public static <T> DemoResult FAIL(String msg,T data){
        return new DemoResult<T>(Constants.FAIL,msg,data);
    }

    /**
     * @Description 转换为map
     * @Author zhaoshouyun
     * @Date 11:31 2020/4/19
     * @Param []
     * @return java.util.Map
     **/
    public  Map filesMap(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",this.code);
        map.put("msg",this.msg);
        map.put("data",this.data);
        return map;
    }
    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("code", code)
                .append("msg", msg)
                .append("data", data)
                .toString();
    }
}
